------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    技能界面

]]--
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 控件
    local Widget = require "widgets/widget"
    local Image = require "widgets/image"
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- image api
    local BASE_ATLAS = "images/widgets/hutao_inspect_hud.xml"
    local BUFF_ICON_ATLAS = "images/widgets/hutao_talent_buff_icon.xml"
    local function get_image(image_name)
        return TUNING.HUTAO_INSPECT_HUD_FNS.get_image(image_name)
    end
    local function get_info(index)
        return TUNING.HUTAO_INSPECT_HUD_FNS.get_info(index)
    end
    local move_debug_event_install = function()    end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 挂载函数，方便动态修改
    TUNING.HUTAO_INSPECT_HUD_FNS.create_skill_page = function(inst,root)            
        ----------------------------------------------------------------------
        --- page 节点
            local page = root:AddChild(Widget())
            page:Hide()
            root.inst:ListenForEvent("page_display",function(_,cmd)
                if cmd == "display_skill_page" then
                    page:Show()
                else
                    page:Hide()
                end
            end)
        ----------------------------------------------------------------------
        --- 出招表
            local tactic_list_box = page:AddChild(Image(get_image("page_skill_tactic_list")))
            tactic_list_box:SetPosition(-230,150)

            local tactic_list_box_str = tactic_list_box:AddChild(Text(CODEFONT,25,"",{ 0/255 , 0/255 ,0/255 , 1}))
            local update_tactic_list_box = function()
                local pt,str,size = get_info("tactic_list")
                if pt and str and size then
                    tactic_list_box_str:SetString(str)
                    tactic_list_box_str:SetSize(size)
                    tactic_list_box_str:SetPosition(pt.x,pt.y)
                end
            end
            update_tactic_list_box()
        ----------------------------------------------------------------------
        --- 点数box
            local page_skill_petals_box = page:AddChild(Image(BASE_ATLAS,"page_skill_petals_box.tex"))
            page_skill_petals_box:SetPosition(490,220)

            local petals_text = page_skill_petals_box:AddChild(Text(CODEFONT,80,"0",{ 0/255 , 0/255 ,0/255 , 1}))
            petals_text:SetPosition(80,0)                
        ----------------------------------------------------------------------
        --- 技能info box
            local page_skill_info_box = page:AddChild(Image(BASE_ATLAS,"page_skill_info_box.tex"))
            page_skill_info_box:SetPosition(-365,-210)
            -- move_debug_event_install(page_skill_info_box,root)

            local info_str = page_skill_info_box:AddChild(Text(CODEFONT,25,"",{ 0/255 , 0/255 ,0/255 , 1}))
            info_str:SetPosition(0,0)
            page.inst:ListenForEvent("mouse_over",function(_,talent)
                local pt,str,size = get_info(talent)
                if pt and str then
                    info_str:SetString(str)
                    info_str:SetPosition(pt.x,pt.y)
                    if size then
                        info_str:SetSize(size)
                    else
                        info_str:SetSize(25)
                    end
                end
            end)
            page.inst:ListenForEvent("mouse_out",function(_,talent)
                -- info_str:SetString("")
            end)
        ----------------------------------------------------------------------
        --- 梅花背景
            local flower_background = page:AddChild(Image(BASE_ATLAS,"page_skill_flower_background.tex"))
            flower_background:SetPosition(270,-70)
            -- move_debug_event_install(flower_background,root)
        ----------------------------------------------------------------------
        --- 花瓣
            local flower_ghost = flower_background:AddChild(ImageButton(BASE_ATLAS,
                "page_skill_flower_ghost.tex",
                "page_skill_flower_ghost.tex",
                "page_skill_flower_ghost.tex",
                "page_skill_flower_ghost.tex",
                "page_skill_flower_ghost.tex"
            ))
            flower_ghost.focus_scale = {1.02, 1.02, 1.02}
            -- flower_ghost:SetTint(1,1,1,0.5)
            flower_ghost:SetPosition(30,-46)
            flower_ghost.ongainfocus = function(flag)
                page.inst:PushEvent("mouse_over","flower_ghost")
            end
            flower_ghost.onlosefocus = function(flag)
                page.inst:PushEvent("mouse_out")
            end

            local flower_petals = {}

            flower_petals[1] = flower_background:AddChild(Image(BASE_ATLAS,"page_skill_flower_1.tex"))
            flower_petals[1]:SetPosition(7,147)

            flower_petals[2] = flower_background:AddChild(Image(BASE_ATLAS,"page_skill_flower_2.tex"))
            flower_petals[2]:SetPosition(180,38)

            flower_petals[3] = flower_background:AddChild(Image(BASE_ATLAS,"page_skill_flower_3.tex"))
            flower_petals[3]:SetPosition(-166,38)

            flower_petals[4] = flower_background:AddChild(Image(BASE_ATLAS,"page_skill_flower_4.tex"))
            flower_petals[4]:SetPosition(-124,-181)

            flower_petals[5] = flower_background:AddChild(Image(BASE_ATLAS,"page_skill_flower_5.tex"))
            flower_petals[5]:SetPosition(135,-183)

            flower_ghost:MoveToFront()
        ----------------------------------------------------------------------
        --- 按钮事件初始化 。包括 info 
            local function button_fns_install(temp_button)
                local talent = temp_button.talent
                temp_button:SetOnClick(function()
                    inst.replica.hutao_com_talent_tree:Unlock(talent)
                end)
                temp_button.ongainfocus = function(flag)
                    page.inst:PushEvent("mouse_over",talent)
                end
                temp_button.onlosefocus = function(flag)
                    page.inst:PushEvent("mouse_out",talent)
                end
            end
        ----------------------------------------------------------------------
        --- 技能图标-高等元素论 skill_button__advanced_element_theory
            local skill_button__advanced_element_theory = flower_background:AddChild(ImageButton(
                BUFF_ICON_ATLAS,
                "skill_button__advanced_element_theory.tex",
                "skill_button__advanced_element_theory.tex",
                "skill_button__advanced_element_theory.tex",
                "skill_button__advanced_element_theory.tex",
                "skill_button__advanced_element_theory.tex"
            ))
            skill_button__advanced_element_theory:SetPosition(7,213)
            -- skill_button__advanced_element_theory:SetOnClick(function()
            --     -- flower_1:Hide()
            -- end)
            skill_button__advanced_element_theory.talent = "advanced_element_theory"
            button_fns_install(skill_button__advanced_element_theory)
        ----------------------------------------------------------------------
        --- 技能图标-神游界外 skill_button__beyond_the_world
            local skill_button__beyond_the_world = flower_background:AddChild(ImageButton(
                BUFF_ICON_ATLAS,
                "skill_button__beyond_the_world.tex",
                "skill_button__beyond_the_world.tex",
                "skill_button__beyond_the_world.tex",
                "skill_button__beyond_the_world.tex",
                "skill_button__beyond_the_world.tex"
            ))
            skill_button__beyond_the_world:SetPosition(240,40)
            -- skill_button__beyond_the_world:SetOnClick(function()
            --     -- flower_2:Hide()
            -- end)
            skill_button__beyond_the_world.talent = "beyond_the_world"
            button_fns_install(skill_button__beyond_the_world)
        ----------------------------------------------------------------------
        --- 技能图标-多多益善 skill_button__the_more_the_better
            local skill_button__the_more_the_better = flower_background:AddChild(ImageButton(
                BUFF_ICON_ATLAS,
                "skill_button__the_more_the_better.tex",
                "skill_button__the_more_the_better.tex",
                "skill_button__the_more_the_better.tex",
                "skill_button__the_more_the_better.tex",
                "skill_button__the_more_the_better.tex"
            ))
            skill_button__the_more_the_better:SetPosition(-223,42)
            -- skill_button__the_more_the_better:SetOnClick(function()
            --     -- flower_3:Hide()
            -- end)
            skill_button__the_more_the_better.talent = "the_more_the_better"
            button_fns_install(skill_button__the_more_the_better)
        ----------------------------------------------------------------------
        --- 技能图标-蝶隐之时 skill_button__time_of_butterfly_concealment
            local skill_button__time_of_butterfly_concealment = flower_background:AddChild(ImageButton(
                BUFF_ICON_ATLAS,
                "skill_button__time_of_butterfly_concealment.tex",
                "skill_button__time_of_butterfly_concealment.tex",
                "skill_button__time_of_butterfly_concealment.tex",
                "skill_button__time_of_butterfly_concealment.tex",
                "skill_button__time_of_butterfly_concealment.tex"
            ))
            skill_button__time_of_butterfly_concealment:SetPosition(-138,-223)
            -- skill_button__time_of_butterfly_concealment:SetOnClick(function()
            --     -- flower_4:Hide()
            -- end)
            skill_button__time_of_butterfly_concealment.talent = "time_of_butterfly_concealment"
            button_fns_install(skill_button__time_of_butterfly_concealment)
        ----------------------------------------------------------------------
        --- 技能图标-血之灶火 skill_button__blood_hearth_fire
            local skill_button__blood_hearth_fire = flower_background:AddChild(ImageButton(
                BUFF_ICON_ATLAS,
                "skill_button__blood_hearth_fire.tex",
                "skill_button__blood_hearth_fire.tex",
                "skill_button__blood_hearth_fire.tex",
                "skill_button__blood_hearth_fire.tex",
                "skill_button__blood_hearth_fire.tex"
            ))
            skill_button__blood_hearth_fire:SetPosition(150,-230)
            -- skill_button__blood_hearth_fire:SetOnClick(function()
            --     -- flower_5:Hide()
            -- end)
            skill_button__blood_hearth_fire.talent = "blood_hearth_fire"
            button_fns_install(skill_button__blood_hearth_fire)
        ----------------------------------------------------------------------
        --- update
            local index_and_talent = {
                [1] = skill_button__advanced_element_theory,
                [2] = skill_button__beyond_the_world,
                [3] = skill_button__the_more_the_better,
                [4] = skill_button__time_of_butterfly_concealment,
                [5] = skill_button__blood_hearth_fire,
            }
            local function param_update_fn()
                --- 剩余天赋点数
                petals_text:SetString(tostring(inst.replica.hutao_com_talent_tree:GetPoints()))
                --- 花瓣和天赋状态
                local showing_num = 0
                for index,temp_button in pairs(index_and_talent) do
                    if inst.replica.hutao_com_talent_tree:IsUnlocked(temp_button.talent) then
                        flower_petals[index]:Show()
                        showing_num = showing_num + 1
                    else
                        flower_petals[index]:Hide()
                    end
                end
                --- 幽灵
                if showing_num == 5 then
                    flower_ghost:Show()
                else
                    flower_ghost:Hide()
                end    
            end
            param_update_fn()
            root.inst:ListenForEvent("hutao_event.talent_tree_client_side_updated",param_update_fn,inst)
        ----------------------------------------------------------------------
        --- 
            return page
        ----------------------------------------------------------------------
    end